/**
 * Created by zk on 9/7/15.
 */
"use strict";
var fs=require('fs');
var sqlite3 = require('sqlite3').verbose();
module.exports = {
    callRecords: function (callback,QueuesInp,AgentsInp,startDate,endDate,CallType,ResponseType,
                           offset,perPage,user,myAgents,myQueues,download,language) {
        fs.stat(sails.config.CDRPath, function (err, stats) {
                if (err) {
                    console.log(err);
                    console.log(" 文件不存在 ");
                } else {
                    var t,t2='',queue_t='',queinp_t='',Ageinp_t='',start_t='',end_t='',CallType_t='',ResponseType_t='';
                    var total;
                    var Calleenum= sails.__({phrase: 'calleenum', locale: language});
                    var Customernum=sails.__({phrase: 'customernum', locale: language});
                    var Agentnum=sails.__({phrase: 'agentnum', locale: language});
                    var Start=sails.__({phrase: 'start', locale: language});
                    var End=sails.__({phrase: 'end', locale: language});
                    var Billsec=sails.__({phrase: 'billsec', locale: language});
                    var Calltype=sails.__({phrase: 'calltype', locale: language});
                    var Disposition=sails.__({phrase: 'disposition', locale: language});
                    var queuenum = myQueues;
                    var agentsNum =myAgents.map(function(item){
                        return item.extension;           //获取所管理坐席的坐席号
                    });
                   /* var agentsName=myAgents.map(function(item){
                        return item.agentName;           //获取所管理坐席的坐席名称
                    });
                    console.log(agentsNum);console.log(queuenum);console.log(QueuesInp);console.log(AgentsInp);
                    console.log(startDate);console.log(endDate);console.log(CallType);console.log(ResponseType);
                    console.log(offset);console.log(perPage);
                    */
                    console.log(language);
                    if (user.isManager) {   /*管理员座席登录*/
                        var db = new sqlite3.Database(sails.config.CDRPath);
                        if(perPage == undefined){        //每页显示数量未定
                            t = "select count(AcctId) as rows from cdr";
                        }else if(!download){             //每页显示数量已定,且点击搜索
                            t = "select * from cdr";
                            t2=" order by start desc limit " + offset + "," + perPage;
                        }else{                           //点击导出
                            t = "select * from cdr";
                            t2=" order by start desc ";
                        }
                        if (queuenum.length != 0) {      //管理员在队列中
                            queue_t = " where (calleenum in (";
                            queuenum.forEach(function (item) { queue_t += "'" + item + "',"; });
                            queue_t = queue_t.substring(0, queue_t.length - 1);
                            queue_t +=") or calleenum ='DOUT')";
                        }else{                           //管理员不在队列
                            queue_t = " where (dst ='" + user.extension + "' or calleenum='DOUT')";
                        }
                        if(QueuesInp!=undefined){        //输入队列号
                            queinp_t = " and  calleenum = '" + QueuesInp + "'";
                        }
                        if(AgentsInp!=undefined){        //输入坐席号
                            switch (CallType) {
                                case "outgoing":
                                    Ageinp_t = " and  src = '" + AgentsInp + "'";
                                    break;
                                case "incoming":
                                case "auto_outgoing":
                                    Ageinp_t = " and  dst = '" + AgentsInp + "'";
                                    break;
                                case undefined:
                                    Ageinp_t = " and ( dst = '" + AgentsInp + "' or src = '" + AgentsInp +"' )";
                            }
                        }
                        if(startDate!=undefined){        //输入开始日期
                            start_t = " and start >= '" + startDate + "'";
                        }
                        if(endDate!=undefined){          //输入结束日期
                            endDate += ' 23:59:59';
                            end_t=" and start <= '" + endDate + "'";
                        }
                        if(CallType!=undefined){         //输入呼叫类型
                            CallType_t = " and calltype ='" + CallType + "'";
                        }
                        if(ResponseType!=undefined){     //输入应答方式
                            ResponseType_t = " and disposition ='" + ResponseType + "'";
                        }
                        t = t + queue_t + queinp_t + Ageinp_t + start_t + end_t + CallType_t + ResponseType_t + t2;
                        console.log(t);

                        db.all(t, function (err, rows) { //查询数据库
                            if (!err) {
                                total = rows;
                                total.map(function(item){//区分呼入还是呼出,交换src和dst
                                    if(item.calltype=="outgoing" ) {
                                        item.agentnum = item.src;
                                        item.customernum = item.dst;
                                        item[Customernum] = item.dst;
                                        item[Agentnum] = item.src;
                                        item[Calleenum]=item.calleenum;
                                        item[Start] = item.start;
                                        item[End] = item.end;
                                        item[Billsec] =item.billsec;
                                        item[Calltype] = sails.__({phrase:item.calltype,locale:language});
                                        item[Disposition] = sails.__({phrase:item.disposition,locale:language});
                                        return item;
                                    }
                                    if(item.calltype=="incoming" || item.calltype== "auto_outgoing"){
                                        item.agentnum=item.dst;  //自动外呼和呼入属于同一类型
                                        item.customernum=item.src;
                                        item[Calleenum]=item.calleenum;
                                        item[Customernum] = item.src;
                                        item[Agentnum] = item.dst;
                                        item[Start] = item.start;
                                        item[End] = item.end;
                                        item[Billsec] =item.billsec;
                                        item[Calltype] = sails.__({phrase:item.calltype,locale:language});
                                        item[Disposition] = sails.__({phrase:item.disposition,locale:language});
                                        return item;
                                    }
                                });
                                    callback(total);
                            } else {
                                console.log(err);
                            }
                            db.close();
                        });
                    } else {     /*普通座席登录*/
                        var db = new sqlite3.Database(sails.config.CDRPath);
                        if(perPage == undefined){        //每页显示数量未定
                            switch (CallType){
                                case "outgoing":
                                    t = "select count(AcctId) as rows from cdr where src='" + user.extension + "'";
                                    break;
                                case "incoming":
                                case "auto_going":
                                    t = "select count(AcctId) as rows from cdr where dst='" + user.extension + "'";
                                    break;
                                case undefined:
                                    t = "select count(AcctId) as rows from cdr where ( src='" + user.extension + "' or dst='" + user.extension + "' )";
                                    break;
                            }
                        }else{                           //每页显示数量已定
                            if(!download) {
                                t2 = " order by start desc limit " + offset + "," + perPage;
                            }else{
                                t2=" order by start desc ";
                            }
                            switch (CallType){
                                case "outgoing":
                                    t = "select * from cdr where src='" + user.extension + "'";
                                    break;
                                case "incoming":
                                case "auto_outgoing":
                                    t = "select * from cdr where dst='" + user.extension + "'";
                                    break;
                                case undefined:
                                    t = "select * from cdr where ( dst='" + user.extension + "' or src='" + user.extension + "' )";
                                    break;
                            }
                        }
                        if(QueuesInp!=undefined){        //输入队列号
                            queinp_t = " and calleenum = '" + QueuesInp + "'";
                        }
                        if(startDate!=undefined){        //输入开始日期
                            start_t = " and start >= '" + startDate + "'";
                        }
                        if(endDate!=undefined){          //输入结束日期
                            endDate += ' 23:59:59';
                            end_t=" and start <= '" + endDate + "'";
                        }
                        if(CallType!=undefined){         //输入呼叫类型
                            CallType_t = " and calltype ='" + CallType + "'";
                        }
                        if(ResponseType!=undefined){     //输入应答方式
                            ResponseType_t = " and disposition ='" + ResponseType + "'";
                        }
                        t = t + queinp_t + start_t + end_t + CallType_t + ResponseType_t + t2;
                        console.log(t);
                        db.all(t, function (err, rows) { //查询数据库
                            if (!err) {
                                total = rows;
                                total.map(function(item){//区分呼入还是呼出,交换src和dst
                                    if(item.calltype=="outgoing" ) {
                                        item.agentnum = item.src;
                                        item.customernum = item.dst;
                                        item[Customernum] = item.dst;
                                        item[Agentnum] = item.src;
                                        item[Calleenum]=item.calleenum;
                                        item[Start] = item.start;
                                        item[End] = item.end;
                                        item[Billsec] =item.billsec;
                                        item[Calltype] = sails.__({phrase:item.calltype,locale:language});
                                        item[Disposition] = sails.__({phrase:item.disposition,locale:language});
                                        return item;
                                    }
                                    if(item.calltype=="incoming" || item.calltype== "auto_outgoing"){
                                        item.agentnum=item.dst;
                                        item.customernum=item.src;
                                        item[Calleenum]=item.calleenum;
                                        item[Customernum] = item.src;
                                        item[Agentnum] = item.dst;
                                        item[Start] = item.start;
                                        item[End] = item.end;
                                        item[Billsec] =item.billsec;
                                        item[Calltype] = sails.__({phrase:item.calltype,locale:language});
                                        item[Disposition] = sails.__({phrase:item.disposition,locale:language});
                                        return item;
                                    }
                                });
                                callback(total);
                            } else {
                                console.log(err);
                            }
                            db.close();
                        });
                    }
                }
        });
    }
};